package com.timepies.button;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;

import com.timepies.ElencoPieActivity;
import com.timepies.R;
import com.timepies.util.Intervallo;

public class PieDatePicker {

	public static EditText addTo(final ElencoPieActivity activity) {

		final Dialog dialog = activity.getEditDialog();
		final EditText testo = (EditText) dialog.findViewById(R.id.data);

		testo.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {

				if (!hasFocus)
					return;

				Calendar now = Calendar.getInstance();
				int year = now.get(Calendar.YEAR);
				int month = now.get(Calendar.MONTH);
				int day = now.get(Calendar.DAY_OF_MONTH);

				DatePickerDialog datePicker = new DatePickerDialog(activity,
						new OnDateSetListener() {
							public void onDateSet(DatePicker datepicker,
									int selectedyear, int selectedmonth,
									int selectedday) {
								Intervallo interval = new Intervallo();
								Calendar newDate = Calendar.getInstance();
								newDate.set(Calendar.YEAR, selectedyear);
								newDate.set(Calendar.MONTH, selectedmonth);
								newDate.set(Calendar.DAY_OF_MONTH, selectedday);
								interval.setStart(newDate.getTime());
								String editedDate = interval.printStartDate();
								testo.setText(editedDate);
								Log.d(this.getClass().getSimpleName(),
										"refreshLis stopt " + editedDate);
							}
						}, year, month, day);
				datePicker.setTitle("Seleziona data");
				datePicker.show();

			}
		});

		return testo;
	}
}
